/**
 * Created by jt on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";

var token = sessionStorage.getItem('token');
var projId = getArgs().id;
App.init();

jQuery(function () {
    console.log('初始化---->',projId);
    $.ajax({
        url: HOST_ADDR+'/cpmp/splrOnline/purchAnsrList',
        type: 'get',
        dataType: 'json',
        contentType:"application/json",
        headers: {
            'access_token': token,
            'login_type':'ums',
            'projId':projId
        },
        success: function(resp) {
            console.log(resp);
            if(resp.success){
                if(resp.data!=null){
                    $(".messageTitle").html('');
                    $(".rightSide").html('');
                    resp.data.length && $.each(resp.data,function(index,item){
                        var count = item.length;
                        var splrNam = item[0].splrNam;
                        var splrId = item[0].splrId;
                        var isActive = (index==0 ? 'active' : '');
                        var htmlTitle = '<li class="msgTitle '+isActive+'" chatTo="'+(index+1)+'"  splrId="'+splrId+'" splrNam="'+splrNam+'">'+splrNam+'(<span class="messageNum">'+count+'</span>)</li>';
                        $(".messageTitle").append(htmlTitle);
                        $(".rightSide").append('<ul class="DetailList hide" chatTo="'+(index+1)+'" splrId="'+splrId+'"></ul>');
                        $.each(item,function(idx,val){
                            var createTim = val.queTime ? val.queTime : val.ansrTime;
                            createTim = createTim ? (new Date(createTim)).Format("yyyy-MM-dd hh:mm:ss") : '';
                            var nameFmt = val.queTime ? splrNam+' '+createTim : '我 '+createTim;
                            var content = val.queContent ? val.queContent : val.ansrContent;
                            var myMsg = val.ansrTime ? 'myMsg' : '';
                            var pubFlg = val.openFlg==1 ? 1 : 0;
                            var pubFlgClass = pubFlg ? 'bg-green' : 'bg-gray';
                            var pubStatus = pubFlg ? '已发布' :  '未发布';
                            var detail = '<li class="detail" msgId="'+val.mid+'" pubFlg="'+pubFlg+'">'+
                                            '<div class="nameFmt">'+nameFmt+'</div>'+
                                            '<div class="fl-left"><span class="'+myMsg+'">'+content+'</span></div>'+
                                            '<div class="fr-right pubSts '+pubFlgClass+'">'+pubStatus+'</div>'+
                                            '</li>';
                            // $(".DetailList[splrId="+splrId+"]").append(detail);
                            $(".DetailList[chatTo="+(index+1)+"]").append(detail);
                        });
                        console.log(htmlTitle);
                    });
                    bindEvent();
                }
            }else{
                console.log(resp.msgCod + ' ' + resp.msgInf);
            }
        },
        error:function(err){
            console.log(err);
        }
    });



    function bindEvent(){
        var tmpsplrId = $(".msgTitle.active").attr("splrId");
        var tmpchatTo = $(".msgTitle.active").attr("chatTo");
        $(".DetailList[chatTo="+tmpchatTo+"]").removeClass("hide").siblings().addClass("hide");
        $(".reply").attr("splrId",tmpsplrId);

        $(".msgTitle").unbind().on("click",function(){
            $(this).addClass("active").siblings().removeClass("active");
            var splrId = $(this).attr("splrId");
            var chatTo = $(".msgTitle.active").attr("chatTo");
            $(".DetailList[chatTo="+chatTo+"]").removeClass("hide").siblings().addClass("hide");
            $(".reply").attr("splrId",splrId);
        });

        //是否公开
        $(".pubSts").unbind().on("click",function(){
            var mid = $(this).parent().attr("msgId");
            var value = parseInt($(this).parent().attr('pubFlg')); // 当前状态
            var toset = value==1 ? 0 : 1;
            var dom = $(this);
            $.ajax({
                url: HOST_ADDR+'/cpmp/splrOnline/openAnsr',
                type: 'post',
                dataType: 'json',
                contentType:"application/json",
                headers: {
                    'access_token': token,
                    'login_type':'ums',
                    'mid':mid,
                    'openFlg':toset
                },
                data:[],
                success:function(resp){
                    console.log(resp);
                    if(resp.success){
                        if(value==0){
                            $(dom).removeClass("bg-gray").addClass("bg-green").html("已发布");
                            $(dom).parent().attr("pubFlg",1);
                            alert("已公开！");
                        }else{
                            $(dom).removeClass("bg-green").addClass("bg-gray").html("未发布");
                            $(dom).parent().attr("pubFlg",0);
                            alert("已撤回！");
                        }
                    }
                },
                error:function(err){
                    console.log(err);
                }
            });
        });
    }


    // 回复
    $(".chat .reply").on("click",function(){
        var splrId = $(this).attr("splrId");
        var splrNam = $(this).attr("splrNam");
        var chatMsg = $(".chatMsg").val();
        var currentDate = new Date().Format("yyyy-MM-dd hh:mm:ss")
        console.log("reply--->",splrId,splrNam,chatMsg);
        if(typeof(splrId)!='undefined' && splrId != ''&& chatMsg != ''){
            var data={
                "ansrContent":chatMsg,
                "splrId":splrId,
                "splrNam":splrNam,
                "projId":projId
            };
            $.ajax({
                url: HOST_ADDR+'/cpmp/splrOnline/ansr',
                type: 'post',
                dataType: 'json',
                contentType:"application/json",
                headers: {
                    'access_token': token,
                    'login_type':'ums'
                },
                data: JSON.stringify(data),
                success: function(resp) {
                    if(resp.success){
                        var messageId = resp.data.mid ? resp.data.mid : 0;
                        var html='<li class="detail" msgId="'+messageId+'" pubFlg="0">'+
                            '<div class="nameFmt">我 '+currentDate+' </div>'+
                            '<div class="fl-left myMsg"><span class="myMsg">'+chatMsg+'</span></div>'+
                            '<div class="fr-right pubSts bg-gray">未发布</div>'+
                            '</li>';
                        $(".DetailList[splrId="+splrId+"]").append(html);
                        $(".chatMsg").val('');
                        var scrolTop = $(".DetailList[splrId="+splrId+"]").height()-$(".rightSide").height();
                        scrolTop = scrolTop>0 ? scrolTop : 0;
                        $(".rightSide").scrollTop(scrolTop);
                        bindEvent();
                    }
                    console.log(resp);
                },
                error:function(err){
                    console.log(err);
                }
            });
        }
    });


});

